package com.lovo.test.JAVAIO.Io;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

public class FileUtil {
    /**
     * properties 读取方法封装
     * @param path 读取路径
     * @param properties 读取到的数据存放的地方
     */
    public static  void propertiesIntPut(String path,Properties properties){
        FileInputStream fis=null;
        try {
            fis=new FileInputStream(path);
            properties.load(fis);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }


    /**
     * properties 输出方法封装
     * @param path 文件储存路径
     * @param properties 要储存的properties对象
     */
    public  static  void propertiesOutPut(String path, Properties properties){
        FileOutputStream fot =null;
        try {
            fot = new FileOutputStream(path);
            properties.store(fot,"");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fot!=null){
                try {
                    fot.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }



    //删除指定文件夹及其里面所有的内容
    public static void deleteDir(String src) {
        File file = new File(src);
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {//判断是不是文件夹
                    deleteDir(files[i].getAbsolutePath());
            } else {//判断是不是文件
                files[i].delete();//删除文件
            }
        }
        file.delete();
    }


    //序列化对象
    public static void writeObject(String path, Object obj) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = new FileOutputStream(path);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(obj);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //反序列化对象
    public static Object readObject(String path) {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = new FileInputStream(path);
            ois = new ObjectInputStream(fis);
            Object obj = (Object) ois.readObject();
            return obj;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

}
